package kr.hkit.powerjava.chapter23;
class Counter{
	private int value;
	
	public synchronized void increment(){value++;}
	public synchronized void decrement(){value--;}
	public synchronized void prnCounter(){System.out.println(value);}
}
class MyThread extends Thread{
	Counter sharedCounter;
	
	public MyThread(Counter shaCounter){
		this.sharedCounter =shaCounter;
	}

	@Override
	public void run() {
		int i=0;
		while(i<20000){
			sharedCounter.increment();
			sharedCounter.decrement();
			if(i% 40==0){
				sharedCounter.prnCounter();
				try {
					sleep((int)(Math.random()*2));
				} catch (InterruptedException e) {}
				
			}
			i++;
		}
	}
		
}



public class CounterTest {
	public static void main(String[] args) {
		Counter c =new Counter();
		new MyThread(c).start();
		new MyThread(c).start();
		new MyThread(c).start();
		new MyThread(c).start();
	}
}
